// pages/cart_real/cart_real.js
const app = getApp()
import { request } from "../../request/index.js";
Page({
  useStore: true,
  /**
   * 页面的初始数据
   */
  data: {
    // 购物车总数
    TitleNum: 0,
    goodsNum: '',
    userInfo: {},
    hasUserInfo: false,
    allSelect: "circle",
    num: 0,
    count: 0,
    slideProductList: [],
    payList: []
  },

  change: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var select = e.currentTarget.dataset.select

    if (select == "circle") {
      var stype = "success"
    } else {
      var stype = "circle"
    }
    var newList = that.data.slideProductList
    newList[index].select = stype
    that.setData({
      slideProductList: newList
    })
    let flag = true
    for (let i = 0; i < this.data.TitleNum; i++) {
      if (this.data.slideProductList[i].select === 'circle') {
        flag = false;
        break
      }
    }
    // 全选
    if (flag) {
      this.setData({
        allSelect: 'success'
      })
    }
    else {
      this.setData({
        allSelect: 'circle'
      })
    }
    that.countNum()
    that.count()
  },

  //全选
  allSelect: function (e) {
    var that = this
    var allSelect = e.currentTarget.dataset.select //先判断是否选中
    var newList = that.data.slideProductList
    console.log(newList)
    if (allSelect == "circle") {
      for (var i = 0; i < newList.length; i++) {
        newList[i].select = "success"
      }
      var select = "success"
    } else {
      for (var i = 0; i < newList.length; i++) {
        newList[i].select = "circle"
      }
      var select = "circle"
    }
    that.setData({
      slideProductList: newList,
      allSelect: select
    })
    that.countNum()
    that.count()
  },

  //计算数量
  countNum: function () {
    var that = this
    var newList = that.data.slideProductList
    var allNum = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].select == "success") {
        allNum++
      }
    }
    parseInt
    that.setData({
      num: allNum
    })
  },

  //计算金额方法
  count: function () {
    var that = this
    var newList = that.data.slideProductList
    var newCount = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].select == "success") {
        newCount += newList[i].bookPrice
      }
    }
    that.setData({
      count: newCount
    })
  },

  // 删除商品
  onClose(event) {
    var that = this
    const { position, instance } = event.detail;
    switch (position) {
      case 'right':
        wx.showModal({
          content: '确定删除吗？',
          cancelText: '取消',
          confirmText: '确认',
          confirmColor: 'red',
          success(res) {
            if (res.confirm) {
              console.log('确认删除')
              console.log(event)
              request({
                url: `/book/cart/${event.target.dataset.cartid}`,
                method: "DELETE",
              })
                .then(result => {
                  console.log('123')
                  request({ url: '/book/cart/1/3' })
                    .then(result => {
                      console.log(result);
                      that.setData({
                        slideProductList: result.data.data.list,
                        TitleNum: result.data.data.total
                      })
                    })
                  instance.close();
                })

            } else if (res.cancel) {
              console.log('取消删除')
              instance.close();
            }
          }
        })
        break;
    }
  },

  // 结算功能
  pay(){
    if(this.data.count){
      //TODO: 付款的功能
      var that = this
      let list = []
      this.data.slideProductList.forEach(r =>{
        if(r.select==='success'){
          list.push(r)
        }
      })
      that.setData({
        payList : list
      })
      

    }
    else{
      wx.showToast({
        title: '还没选择宝贝哦',
        icon: 'none',
        duration: 2000
      })
    }
  },

  goIndex(){
    wx.reLaunch({
      url: '../index/index',
      success: (result) => {
      },
      fail: (err) => {console.log(err)},
      complete: () => {}
    });
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    request({ url: '/book/cart/1/3' })
      .then(result => {
        console.log(result);
        let list = result.data.data.list
        list.forEach((r) => {
          r.select = 'circle';
        })
        console.log(list)
        that.setData({
          slideProductList: list,
          TitleNum: result.data.data.list.length
        })
        console.log(this.data.slideProductList)
      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  methods: {


  },

})